using System.IO;
using System.Text;

namespace Engine.FileFormats3D.C11Syndikat.QADFile
{
    public class ObjectPropertie
    {
        #region Private Fields

        private ObjectCollisionModel collisionModel;
        private ObjectName collisionSound;
        private ObjectName fallSound;
        private string name;
        private long positionInFile;
        private ObjectType type;
        private byte[] unknown = new byte[12];
        private byte[] unknown2 = new byte[16];
        private byte[] unknown3 = new byte[16];
        private int weight;

        #endregion Private Fields

        #region Internal Constructors

        internal ObjectPropertie(string name)
        {
            this.collisionModel = ObjectCollisionModel.Self;
            this.collisionSound = new ObjectName("none");
            this.fallSound = new ObjectName("none");
            this.name = name;
            this.type = ObjectType.Static;
        }

        internal ObjectPropertie()
        {
        }

        #endregion Internal Constructors

        #region Public Methods

        public static string ByteToString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i].ToString() + " ");
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(this.name + " At Position; " + this.positionInFile.ToString());
            sb.AppendLine("=================");
            sb.AppendLine("type:            " + this.type.ToString());
            sb.AppendLine("CollisionModel:  " + this.collisionModel.ToString());
            sb.AppendLine("Weight:          " + this.weight.ToString());
            sb.AppendLine("unknown :        " + ByteToString(this.unknown));
            sb.AppendLine("Collision Sound: " + this.collisionSound.ToString());
            sb.AppendLine("unknown2 :       " + ByteToString(this.unknown2));
            sb.AppendLine("Fall Sound:      " + this.fallSound.ToString());
            sb.AppendLine("unknown3 :       " + ByteToString(this.unknown3));

            return sb.ToString();
        }

        #endregion Public Methods

        #region Internal Methods

        internal static ObjectPropertie FromStream(BinaryReader br, string name)
        {
            ObjectPropertie properties = new ObjectPropertie();

            properties.positionInFile = br.BaseStream.Position;
            properties.name = name;

            properties.type = (ObjectType)br.ReadInt16();
            properties.collisionModel = (ObjectCollisionModel)br.ReadInt16();
            properties.weight = br.ReadInt32();
            properties.unknown = br.ReadBytes(properties.unknown.Length);//12 bytes
            properties.collisionSound = ObjectName.FromStream(br);
            properties.unknown2 = br.ReadBytes(properties.unknown2.Length);//16 bytes
            properties.fallSound = ObjectName.FromStream(br);
            properties.unknown3 = br.ReadBytes(properties.unknown3.Length);//16 bytes

            return properties;
        }

        internal void Save(BinaryWriter bw)
        {
            bw.Write((ushort)this.type);// properties.type = (ObjectType)br.ReadInt16();
            bw.Write((ushort)this.collisionModel);// properties.collisionModel = (ObjectCollisionModel)br.ReadInt16();
            bw.Write((uint)this.weight);// properties.weight = br.ReadInt32();
            bw.Write(this.unknown);// properties.unknown = br.ReadBytes(properties.unknown.Length);//12 bytes
            this.collisionSound.Save(bw);// properties.collisionSound = ObjectName.LoadFromStream(br);
            bw.Write(this.unknown2);// properties.unknown2 = br.ReadBytes(properties.unknown2.Length);//16 bytes
            this.fallSound.Save(bw);// properties.fallSound = ObjectName.LoadFromStream(br);
            bw.Write(this.unknown3);// properties.unknown3 = br.ReadBytes(properties.unknown3.Length);//16 bytes
        }

        #endregion Internal Methods
    }
}